public class BinarySearchTree {
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;

    public void insert(int key){
        if(root==null){
            root=new TreeNode(key);
            return ;
        }
        TreeNode parent = null;
        TreeNode cur = root;
        TreeNode node = new TreeNode(key);
        while(cur!=null){
            if(cur.val<key){
                parent=cur;
                cur=cur.right;
            }else{
                if(cur.val>key){
                    parent=cur;
                    cur=cur.left;
                }else return ;
            }
        }
        if(parent.val>key){
            parent.left=node;
        }else parent.right=node;
    }

    public TreeNode search(int val){ //搜索树中val值的节点
        TreeNode cur = root;
        while(cur!=null){
            if(cur.val<val){
                cur=cur.right;
            }else{
                if(cur.val>val){
                    cur=cur.left;
                }else return cur;
            }
        }
        return null;
    }

    public void remove(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if(cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else if(cur.val > key) {
                parent = cur;
                cur = cur.left;
            }else {
                removeNode(parent,cur);
                return;
            }
        }
    }

    private void removeNode(TreeNode parent, TreeNode cur) {
        if(cur.left == null) {
            if(cur == root) {
                root = cur.right;
            }else if(parent.left == cur) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if(cur.right == null) {
            if(cur == root) {
                root = cur.left;
            }else if(parent.left == cur) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            TreeNode targetParent = cur;
            TreeNode target = cur.right;
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if(targetParent.left == target) {
                targetParent.left = target.right;
            }else {
                targetParent.right = target.right;
            }
        }
    }





    public TreeNode pre;
    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree==null) return null;
        ConvertChild2(pRootOfTree);
        TreeNode prevv=pre;
        ConvertChild(pRootOfTree);
        return prevv;
    }
    public void ConvertChild(TreeNode c){
        if(c==null) return ;
        ConvertChild(c.left);
        if(pre!=c){
            pre.right = c;
            c.left=pre;
            pre=c;
        }
        ConvertChild(c.right);
    }
    private void ConvertChild2(TreeNode node){
        if(node==null) return;
        ConvertChild2(node.left);
        if(pre!=null)
            return ;
        pre=node;
        return ;
    }
}
